//用于创建整个应用的路由器
import VueRouter from "vue-router"
//引入路由组件
import Search from '@/pages/Search'
import Login from '@/pages/Login'
import Register from '@/pages/Register'
import Detail from '@/pages/Detail'
import AddCartSuccess from '@/pages/AddCartSuccess'
import ShopCart from '@/pages/ShopCart'
import Center from '@/pages/Center'
import Trade from '@/pages/Trade'
import Pay from '@/pages/Pay'
import MyOrder from '@/pages/Center/myOrder'
import MyDetail from '@/pages/Center/myDetail'
import ChangePw from '@/pages/Center/changePw'
import MyCollect from '@/pages/Center/myCollect'
import AfterSales from '@/pages/Center/afterSales'
import MyAfterSales from '@/pages/Center/myAfterSales'
import MyHistoryOrder from '@/pages/Center/myHistoryOrder'
//引入store
import store from '@/store'

//备份VueRouter原型对象push
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;
//重写push函数
//第一个参数：告诉push往哪跳转（传递哪些参数）
VueRouter.prototype.push = function (location, resolve, reject) {
    if (resolve && reject) {
        originPush.call(this, location, resolve, reject);
    } else {
        originPush.call(this, location, () => { }, () => { });
    }
}
VueRouter.prototype.replace = function (location, resolve, reject) {
    if (resolve && reject) {
        originReplace.call(this, location, resolve, reject);
    } else {
        originReplace.call(this, location, () => { }, () => { });
    }
}
//创建一个路由器
let router = new VueRouter({
    routes: [
        {
            path: '/',
            redirect: '/search',
        },
        {
            path: '/center',
            component: Center,
            meta: { show: true },
            //二级路由
            children: [
                {
                    path: 'myorder',
                    component: MyOrder,
                },
                {
                    path: 'myhistoryorder',
                    component: MyHistoryOrder,
                },
                {
                    path: '/center',
                    redirect: '/center/myorder'
                },
                {
                    path: 'mydetail',
                    component: MyDetail
                },
                {
                    path: 'changepw',
                    component: ChangePw
                },
                {
                    path: 'mycollect',
                    component: MyCollect,
                },
                {
                    path: 'aftersales',
                    component: AfterSales,
                },
                {
                    path: 'myaftersales',
                    component: MyAfterSales,
                }

            ]
        },
        {
            path: '/shopcart',
            component: ShopCart,
            meta: { show: true }
        },
        {
            path: '/trade',
            component: Trade,
            meta: { show: true }
        },
        {
            path: '/pay',
            component: Pay,
            name: 'pay',
            meta: { show: true }
        },
        {
            path: '/addcartsuccess',
            name: 'addcartsuccess',
            component: AddCartSuccess,
            meta: { show: true }
        },
        {
            path: '/detail/:waresId',
            component: Detail,
            meta: { show: true }
        },
        {
            path: '/search/:keyword?',
            component: Search,
            meta: { show: true },
            name: "search"
        },
        {
            path: '/login',
            component: Login,
            name: "login"
        },
        {
            path: '/register',
            component: Register
        },
    ]
})
//全局守卫,前置守卫(路由跳转前判断)
router.beforeEach(async (to, from, next) => {
    //to:获取跳转到哪个路由
    //from:获取从哪个路由来
    //next:放行函数
    //获取用户信息
    // let name = store.state.user.buyerInfo.name;
    //判断用户是否登录
    let token = store.state.user.token;
    if (token) {
        let result = await store.dispatch('getBuyerInfo');
        if (result == null) {
            await store.dispatch('buyerLogout');
            next('/login');
        }
    }

    if (!token) {
        if (to.path.indexOf('/center') != -1) {
            next('/login');
        } else if (to.path == '/shopcart') {
            next('/login');
        } else {
            next();
        }
    } else {
        let result = await store.dispatch('getBuyerInfo');
        if (result == null) {
            await store.dispatch('buyerLogout');
            next('/login');
        } else if (to.path == '/login') {
            next('/');
        } else {
            next();
        }
    }
});
export default router